package com.eaxi.a.smsproxy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Observable;
import java.util.Observer;

public class ScrollingActivity extends AppCompatActivity implements Observer {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        WebView webView = (WebView) findViewById(R.id.wv1);
        WebSettings wSet = webView.getSettings();
        wSet.setUseWideViewPort(true);
        wSet.setJavaScriptEnabled(true);
        wSet.setDomStorageEnabled(true);
        wSet.setLoadWithOverviewMode(true);
        wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.addJavascriptInterface(this, "SmsProxy");
        webView.removeJavascriptInterface("searchBoxJavaBridge_");
        webView.removeJavascriptInterface("accessibility");
        webView.removeJavascriptInterface("accessibilityTraversal");

        // Toast.makeText(getApplicationContext(), "Screen height="+metrics.heightPixels, Toast.LENGTH_LONG).show();
//        webView.loadUrl("file:///android_asset/web/index.html");
        webView.loadUrl("https://dh.eaxi.com/smsproxy/");

        // 检查权限

        ObservableObject.getInstance().addObserver(this);
    }

    @JavascriptInterface
    public void sendSms(String to, String body) {
        Log.i("JSI", "sending sms " + to + ", body=" + body);
        // Toast.makeText(getApplicationContext(), "Sending Message", Toast.LENGTH_LONG).show();
        SmsManager smsManager = SmsManager.getDefault();
        if (to == null || body == null || to.isEmpty() || body.isEmpty()) return;
        try {
            smsManager.sendTextMessage(to, null, body, null, null);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Can not send message: " + e.getLocalizedMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void update(Observable o, Object data) {
        WebView webView = (WebView) findViewById(R.id.wv1);

        SmsObject smsMessage = (SmsObject) data;

        // New message received
        String sender = smsMessage.getSender();
        String body = smsMessage.getBody();

        String script = String.format("uploadMessage('%s', '%s')", sender, body);
        webView.evaluateJavascript(script, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
            //Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
            }
        });
    }
}
